package com.zdkj.subway.check.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.entity.SwAuConductorHierarchyAmountDifference;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuConductorHierarchyAmountDifferenceMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
/***
 * 线网层级确认-数据协查管理-BOM票款差异协查-BOM系统差异协查-BOM层级差异
 * BOM层级差异（售票员票款层级差异 SW_AU_CONDUCTOR_HIERARCHY_AMOUNT_DIFFERENCE）
 */
@Service
public class SwAuConductorHierarchyAmountDifferenService {

    @Resource
    private SwAuConductorHierarchyAmountDifferenceMapper swAuConductorHierarchyAmountDifferenceMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 售票员票款差异列表
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNoticeList(JSONObject jsonObject) {
        //分页
        Integer startPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");
        //线路站点
        String lineCode = jsonObject.getString("lineCode");
        String stationCode = jsonObject.getString("stationCode");
        //售票员
        String conductorPersonName = jsonObject.getString("conductorPersonName");
        String conductorPersonCode = jsonObject.getString("conductorPersonCode");
        //通知时间
        String assistTime = jsonObject.getString("assistTime");
        String settleTime = jsonObject.getString("settleTime");
        String assistState = jsonObject.getString("assistState");
        String assistStates = jsonObject.getString("assistStates");
        String jdReplyReason = jsonObject.getString("jdReplyReason");

        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        //金额范围
        BigDecimal diffInvestStartAmount = jsonObject.getBigDecimal("diffInvestStartAmount");
        BigDecimal diffInvestEndAmount = jsonObject.getBigDecimal("diffInvestEndAmount");
        BigDecimal diffSelltkStartAmount = jsonObject.getBigDecimal("diffSelltkStartAmount");
        BigDecimal diffSelltkEndAmount = jsonObject.getBigDecimal("diffSelltkEndAmount");
        BigDecimal diffFaretkStartAmount = jsonObject.getBigDecimal("diffFaretkStartAmount");
        BigDecimal diffFaretkEndAmount = jsonObject.getBigDecimal("diffFaretkEndAmount");
        //设备号
        String equipmentNumber = jsonObject.getString("equipmentNumber");
        //清分回馈状态
        String qfReplyStatus = jsonObject.getString("qfReplyStatus");
        //机电回馈状态
        String jdReplyStatus = jsonObject.getString("jdReplyStatus");

        Example example = new Example(SwAuConductorHierarchyAmountDifference.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(assistTime)) {
            criteria.andLike("assistTime", assistTime+"%");
        }
        if (!StringUtils.isEmpty(assistState)) {
            criteria.andEqualTo("assistState", assistState);
        }
        if (!StringUtils.isEmpty(assistStates)) {
            List assistStatesList = Arrays.asList(assistStates.split(","));//根据逗号分隔转化为list
            criteria.andIn("assistState", assistStatesList);
        }
        if (!StringUtils.isEmpty(lineCode)) {
            criteria.andEqualTo("lineCode", lineCode);
        }
        if (!StringUtils.isEmpty(stationCode)) {
            criteria.andEqualTo("stationCode", stationCode);
        }
        if (!StringUtils.isEmpty(jdReplyReason)) {
            criteria.andEqualTo("jdReplyReason", jdReplyReason);
        }
        if (!StringUtils.isEmpty(settleTime)) {
            criteria.andEqualTo("settleTime", settleTime);
        }
        example.setOrderByClause("ASSIST_TIME desc");
        if(StringUtils.isEmpty(lineCode)) {
            PigxUser user = SecurityUtils.getUser();
            Map<String, Object> p1 = new HashMap<>();
            p1.put("user_code", user.getUserCode());
            List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
            SysUser sysUser = sysUsers.get(0);
            String managementLineStation = sysUser.getManagementLineStation();
            //管辖线路
            if (!StringUtils.isEmpty(managementLineStation)) {
                JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
                List<String> LineObjects = new ArrayList<>();
                for (int i = 0; i < managementLineStations.size(); i++) {
                    JSONObject jsonObject1 = managementLineStations.getJSONObject(i);
                    lineCode = jsonObject1.getString("line");
                    LineObjects.add(lineCode);
                }
                criteria.andIn("lineCode", LineObjects);
            }else {
                //不管辖线路
                criteria.andEqualTo("lineCode", sysUser.getLineCode());
            }
        }
        if (!StringUtils.isEmpty(conductorPersonName)) {
            criteria.andEqualTo("conductorPersonName", conductorPersonName);
        }
        if (!StringUtils.isEmpty(conductorPersonCode)) {
            criteria.andEqualTo("conductorPersonCode", conductorPersonCode);
        }
        if (!StringUtils.isEmpty(equipmentNumber)) {
            criteria.andEqualTo("equipmentNumber", equipmentNumber);
        }
        if (!StringUtils.isEmpty(qfReplyStatus)) {
            criteria.andEqualTo("qfReplyStatus", qfReplyStatus);
        }
        if (!StringUtils.isEmpty(jdReplyStatus)) {
            criteria.andEqualTo("jdReplyStatus", jdReplyStatus);
        }
        if (diffFaretkStartAmount != null && diffFaretkEndAmount != null) {
            criteria.andBetween("diffFaretkAmount", diffFaretkStartAmount.multiply(new BigDecimal(100)), diffFaretkEndAmount.multiply(new BigDecimal(100)));
        }
        if (diffSelltkStartAmount != null && diffSelltkEndAmount != null) {
            criteria.andBetween("diffSelltkAmount", diffSelltkStartAmount.multiply(new BigDecimal(100)), diffSelltkEndAmount.multiply(new BigDecimal(100)));
        }
        if (diffInvestStartAmount != null && diffInvestEndAmount != null) {
            criteria.andBetween("diffInvestAmount", diffInvestStartAmount.multiply(new BigDecimal(100)), diffInvestEndAmount.multiply(new BigDecimal(100)));
        }
        criteria.andEqualTo("comparativeType", jsonObject.getString("comparativeType"));
        JSONObject resultJson = new JSONObject();
        if(startPage!=null){
            PageHelper.startPage(startPage,pageSize);
        }
        List<SwAuConductorHierarchyAmountDifference> swAuConductorHierarchyAmountDifferences =
                swAuConductorHierarchyAmountDifferenceMapper.selectByExample(example);
        if(startPage!=null){
            PageInfo<SwAuConductorHierarchyAmountDifference> swAuConductorHierarchyAmountDifferencePageInfo
                    = new PageInfo<>(swAuConductorHierarchyAmountDifferences);
            resultJson.put("pageInfo",swAuConductorHierarchyAmountDifferencePageInfo);
        }
        resultJson.put("list",swAuConductorHierarchyAmountDifferences);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    /**
     * 售票员票款层级差异 单独查询
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNotice(JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        SwAuConductorHierarchyAmountDifference swAuConductorHierarchyAmountDifference =
                swAuConductorHierarchyAmountDifferenceMapper.selectByPrimaryKey(id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("obj",swAuConductorHierarchyAmountDifference);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    public Result updateSwAuConductorNotice(JSONObject jsonObject) {
        SwAuConductorHierarchyAmountDifference swAuConductorHierarchyAmountDifference =
                JSONObject.parseObject(jsonObject.toJSONString(), SwAuConductorHierarchyAmountDifference.class);
        int i = swAuConductorHierarchyAmountDifferenceMapper.updateByPrimaryKeySelective(swAuConductorHierarchyAmountDifference);
        return new Result(ResultDetailMsg.OK);
    }
}
